package com.appkubes.mall.restful.application.user.hierarchy.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.core.controller.BaseMallUserHierarchyController;
import com.appkubes.mall.core.entity.MallPromoter;
import com.appkubes.mall.core.entity.MallUser;
import com.appkubes.mall.core.entity.MallUserHierarchy;
import com.appkubes.mall.core.entity.MallUserUtm;
import com.appkubes.mall.core.service.BaseMallPromoterService;
import com.appkubes.mall.restful.application.user.hierarchy.entity.dto.UserHierarchyDTO;
import com.appkubes.mall.restful.application.user.hierarchy.entity.dto.UserHierarchyParentDTO;
import com.appkubes.mall.restful.application.user.hierarchy.entity.vo.UserHierarchyGetVO;
import com.appkubes.mall.restful.application.user.hierarchy.service.UserHierarchyService;
import com.appkubes.mall.restful.application.user.user.service.UserService;
import com.appkubes.mall.restful.application.utm.service.UserUtmService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@Api(tags = "用户账户层级关系(UserHierarchyController)")
@RestController
@RequestMapping("/user/hierarchy")
public class UserHierarchyController extends BaseMallUserHierarchyController {

    @Resource
    UserHierarchyService userHierarchyService;
    @Resource
    UserService userService;
    @Resource
    UserUtmService userUtmService;
    @Resource
    BaseMallPromoterService promoterService;

    /**
     * 获取用户推荐的所有下级用户
     *
     * @param userHierarchyGetVO
     * @return
     */
    @ApiOperation("获取用户的一级推广用户")
    @GetMapping({"/getListByParentKey", "/getList"})
    public Response<IPage<UserHierarchyDTO>> getListByParentKey(@ParamValid UserHierarchyGetVO userHierarchyGetVO) {
        IPage<UserHierarchyDTO> hierarchyIPage = userHierarchyService.getListByParentKey(userHierarchyGetVO);
        return Response.success(hierarchyIPage);
    }

    /**
     * 获取用户的二级推广用户信息
     * @param userHierarchyGetVO
     * @return
     */
    @ApiOperation("获取用户的二级推广用户")
    @GetMapping("/getListByParentKey2")
    public Response<IPage<UserHierarchyDTO>> getListByParentKey2(@ParamValid UserHierarchyGetVO userHierarchyGetVO){
        IPage<UserHierarchyDTO> hierarchyIPage = userHierarchyService.getListByParentKey2(userHierarchyGetVO);
        return Response.success(hierarchyIPage);
    }

    /**
     * 获取用户的被推荐信息(上级)
     *
     * @return
     */
    @ApiOperation("获取用户的推广信息")
    @GetMapping("/getParent/{userKey}")
    public Response getHierarchy(@PathVariable("userKey") String userKey) {
        UserHierarchyParentDTO parentDTO = new UserHierarchyParentDTO();
        MallUserHierarchy userHierarchy = userHierarchyService.getById(userKey);
        if (userHierarchy != null && userHierarchy.getParentUserKey() != null) {
            //取得上级用户信息
            MallUser parentUser = userService.getById(userHierarchy.getParentUserKey());
            MallUserUtm userUtm = userUtmService.getById(userKey);

            //获取上级的基础信息
            parentDTO.setUserKey(userKey);
            if (parentUser != null) {
                parentDTO.setParentUserKey(parentUser.getUserKey());
                parentDTO.setParentUserName(parentUser.getName());
                parentDTO.setParentTel(parentUser.getTel());
            }


            //获取推广员信息
            MallPromoter promoter = promoterService.getById(userHierarchy.getPromoterKey());
            if (promoter != null) {
                parentDTO.setPromoterName(promoter.getPromoterName());
            }

            //获取渠道采集信息
            parentDTO.setUtmSource(userUtm.getUtmSource());
            parentDTO.setPlatformType(userUtm.getPlatformType());

        }
        return Response.success(parentDTO);
    }
}
